#
# You will need the DevWisdom plugin to run this.
#
class MigrateLockDownAccess < ActiveRecord::Migration
  class << self
      
    def up
      setup_roles
      add_public_system_object
      add_user_management_methods
      add_role_management_methods
      add_system_object_management_methods
      add_user_group_management_methods
    end
    
    def down
      remove_system_methods user_group_methods
      remove_system_methods system_object_methods
      remove_system_methods role_methods
      remove_system_methods public_methods
      remove_system_object "Public"
    end
    
    private
      def setup_roles
        @@roles = Role.find(:all)
        @@admin = Role.send LockDown::ROLES.index(1)
      end
      
      def add_public_system_object
        create_static_system_object("Public", public_methods, @@roles)
      end
      
      def public_methods
        %w(sessions/create sessions/destroy sessions/denied)
      end
      
      def add_user_management_methods
        LockDown::ROLES.each do |key,value|
          methods = []
          methods << "users/list_#{Inflector.pluralize(key.to_s)}" 
          methods << "users/new_#{key.to_s}"
          methods << "users/show_#{key.to_s}"
          methods << "users/edit_#{key.to_s}"
          methods << "users/create_#{key.to_s}"
          methods << "users/update_#{key.to_s}"
          methods << "users/delete_#{key.to_s}"
          create_system_object("#{key.to_s.capitalize} Management", methods, @@admin)
        end
      end
      
      def add_role_management_methods
        create_system_object("Role Management", role_methods, @@admin)
      end
      
      def role_methods
        rest_urls_for "roles"
      end
      
      def add_system_object_management_methods
        create_system_object("System Object Management", system_object_methods, @@admin)
      end
      
      def system_object_methods
        rest_urls_for "system_objects"
      end
      
      def add_user_group_management_methods
        create_system_object("User Group Management", user_group_methods, @@admin)
      end
      
      def user_group_methods
        rest_urls_for "user_groups"
      end
  end
end
